(function () {
    angular.module('vehicleCheckApp').controller('TransporterCtrl', ['$scope','$state','$ionicScrollDelegate','_DBSerive' , TransporterCtrl]);

    function TransporterCtrl($scope, $state, $ionicScrollDelegate, _DBSerive) {
        _DBSerive.queryTransport().then(function(results){
			$scope.transporter=[];
            $scope.transporterList = [];//this list is for search function
			var itLetterList=[];
			for (var i = 0; i < results.rows.length; i++){
                var itLetter = ConvertPinyin(results.rows.item(i).companyname).substring(0,1);
				if(itLetterList.indexOf(itLetter) <= -1)
					itLetterList.push(itLetter);
                    $scope.transporterList.push({
                        companyname:results.rows.item(i).companyname,
                        companycode: results.rows.item(i).companycode,
                        new_companyname: ConvertPinyin(results.rows.item(i).companyname)+results.rows.item(i).companyname,
                        id:results.rows.item(i).id
                    });

			};
			itLetterList = itLetterList.sort();
			for(var i = 0; i < itLetterList.length; i++){
				var transporterObj = {};
				transporterObj.title = itLetterList[i];
				transporterObj.list = [];
				for (var j = 0; j < results.rows.length; j++){
                    var itLetter = ConvertPinyin(results.rows.item(j).companyname).substring(0,1);
					if(itLetter == itLetterList[i]){
						transporterObj.list.push({
                            companyname: results.rows.item(j).companyname,
							companycode: results.rows.item(j).companycode,
							new_companyname:ConvertPinyin(results.rows.item(j).companyname)+results.rows.item(j).companyname,
							id:results.rows.item(j).id
						});
					}
				}
				$scope.transporter.push(transporterObj);
			}
		});		

        $scope.scrollTo = function(list) {  
            var alphaletter=list.title;
			var array = $scope.transporter;            
            var distance=0;
            for (var i = 0; i < array.length; i++) {
                if(array[i].title == alphaletter){ 
                    break;
                }
                var length= array[i].list.length;
                distance = distance + length + 1;
            }           
           $ionicScrollDelegate.$getByHandle('mainScroll').scrollTo(0, distance*54, true);            
        };

        $scope.selectTransporter = function(item){
            console.log(item);
			$state.go("vehicleCheck",{transporter:item});
        }


        //清除搜索框
        $scope.removeCondition = function(condition){
          $scope.condition = "";
        }
    };
})();